package com.example.http.provider.common;

import com.example.http.provider.bean.User;
import com.thoughtworks.xstream.XStream;

public class XStreamUtil {
    private static final XStream STREAM = new XStream();

    static {
        STREAM.alias("Result", Result.class);
        STREAM.alias("User", User.class);
        XStream.setupDefaultSecurity(STREAM);
        STREAM.allowTypes(new Class[]{Result.class, User.class});
    }

    private XStreamUtil() {

    }

    @SuppressWarnings("unchecked")
    public static <T> T fromXML(String xml, Class<T> clazz) {
        return (T) STREAM.fromXML(xml);
    }

    public static String toXML(Object data) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + STREAM.toXML(data);
    }
}
